import express from "express";
import cors from "cors";
import { mockData } from "./mock/data.js";

const app = express();
app.use(cors());
app.use(express.json());

app.get("/:api", (req, res) => {
    const { api } = req.params;
    const data = mockData[api];

    if (!data) {
        return res.status(404).json({ code: 404, msg: "接口不存在", data: null });
    }

    const msg = "success"; // 或者根据需要自定义 msg

    if (Array.isArray(data)) {
        const page = parseInt(req.query.page) || 1;
        const pageSize = parseInt(req.query.pageSize) || data.length;
        const start = (page - 1) * pageSize;
        const end = start + pageSize;

        res.json({
            code: 200,
            msg,
            data: data.slice(start, end),
            total: data.length
        });
    } else {
        res.json({
            code: 200,
            msg,
            data
        });
    }
});


app.listen(3000, () => {
    console.log("✅ Mock server running on http://localhost:3000");
});
